﻿@using SapaFinance.DTO
@using Telerik.Web.Mvc.UI
@model IEnumerable<CFOTree>
@{
    Layout = null;
}
@{
    var selectedCfo = @Model.First().SelectedItems;
    @(Html.Telerik().TreeView()
               .Name("CFOTree")
               .ShowCheckBox(true)
               .BindTo(@Model, mappings =>
               {
                   mappings.For<CFOTree>(binding => binding.ItemDataBound((item, cfoTree) =>
                                                                                    {
                                                                                        item.Text = "Отделы";
                                                                                        item.Checkable = false;
                                                                                        item.Expanded = false;
                                                                                    })
                                                                                    .Children(x => x.Items));

                   mappings.For<TreeViewItem>(binding => binding.ItemDataBound((item, src) =>
                                                                         {
                                                                             item.Text = src.Text;
                                                                             item.Value = src.Value;
                                                                             if (selectedCfo != null)
                                                                             {
                                                                                 var checkedNode = selectedCfo
                                                                                     .Where(e => e.Value.Equals(src.Value))
                                                                                     .FirstOrDefault();
                                                                                 item.Checked = checkedNode != null && checkedNode.Checked;
                                                                             }
                                                                         }));
               }))
}
